package com.zyd.blog.personal.service.impl;
import java.util.Date;
import	java.util.HashMap;

import com.zyd.blog.persistence.mapper.MyCompanyInfoMapper;
import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.po.CompanyInfoPo;
import com.zyd.blog.personal.service.MyCompanyInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 公司信息业务逻辑层
 * @author mfy
 */
@Service
public class MyCompanyInfoServiceImpl implements MyCompanyInfoService {

    @Resource
    private MyCompanyInfoMapper myCompanyInfoMapper;

    @Override
    public Boolean isHaveMyCompanyInfo(Long id, String model) {
        if(id == null){
            if(model == null || "".equals(model)){
                return false;
            }
        }
        CompanyInfoPo myCompanyInfo = getMyCompanyInfo(id,model);
        return myCompanyInfo != null;
    }

    @Override
    public CompanyInfoPo getMyCompanyInfo(Long id, String model) {
        if(id == null){
            if(model == null || "".equals(model)){
                return null;
            }
        }
        Map<String,Object> condition = new HashMap<> (16);
        condition.put("id",id);
        condition.put("model",model);
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        List<CompanyInfoPo> myCompanyInfos = getAllMyCompanyInfo(condition);
        if(myCompanyInfos == null || myCompanyInfos.size() == 0){
            return null;
        }
        return myCompanyInfos.get(0);
    }

    @Override
    public List<CompanyInfoPo> getAllMyCompanyInfo(Map<String, Object> map) {
        return myCompanyInfoMapper.queryMyCompanyInfoByCondition(map);
    }

    @Override
    public Boolean updateMyCompanyInfo(CompanyInfoPo companyInfo) {
        Long id = companyInfo.getId();
        String model = companyInfo.getModel();
        if(id == null){
            if(model == null || "".equals(model)){
                return false;
            }
        }
        companyInfo.setUpdateTime((new Date()).getTime());
        return myCompanyInfoMapper.updateMyCompanyInfo(companyInfo) > 0;
    }

    @Override
    public Boolean addMyCompanyInfo(CompanyInfoPo companyInfo) {
        String model = companyInfo.getModel();
        if(model == null || "".equals(model)){
            return false;
        }
        companyInfo.setUpdateTime((new Date()).getTime());
        companyInfo.setAddTime((new Date()).getTime());
        return myCompanyInfoMapper.addMyCompanyInfo(companyInfo) > 0;
    }
}
